﻿using System;

namespace DM4
{
    public class OperationResult
    {     
        private int _result = DM4Consts.ErrorUnknown;
        public string Message = "Unknown Error";

        private string _usrSessionId;

        public string UsrSessionId
        {
            get
            {
                return _usrSessionId;
            }
            set
            {
                if (_usrSessionId == value)
                    return;
                _usrSessionId = value;
            }
        }

        private string _lastAccess;

        public string LastAccess
        {
            get
            {
                return _lastAccess;
            }
            set
            {
                if (_lastAccess == value)
                    return;
                _lastAccess = value;
            }
        }

        public OperationResult() { }

        public OperationResult(int result)
        {
            Result = result;
        }

        public OperationResult(OperationResult main, int value):this(value)
        {
            LastAccess = main.LastAccess;
            UsrSessionId = main.UsrSessionId;
        }

        public OperationResult(int result, string message)
        {
            Result = result;
            Message = message;
        }

        public int Result
        {
            get
            {
                return _result;
            }
            set
            {
                _result = value;
                if (value == DM4Consts.Ok)
                    Message = "Operation complete!";
                else if (Message == "Operation complete!")
                    Message = "Unknown Error";
            }
        }

        public void SetValue(int result, string lastAccess, string usrSessionId)
        {
            _result = result;
            LastAccess = lastAccess;
            UsrSessionId = usrSessionId;
        }

        public static bool operator true(OperationResult value) 
        { 
            return value.Result >= 0;
        }

        public static bool operator false(OperationResult value) 
        {
            return value.Result < 0;
        }

        public static bool operator !(OperationResult value)
        {
            return value.Result < 0;
        }

        public static OperationResult ErrorSessionId(string usrSessionId)
        {
            return new OperationResult(DM4Consts.ErrorClientAccess, String.Format("User SessionId: {0} not found.", usrSessionId));
        }
    }
}
